package com.achobeta.recruitment.utils;

import com.achobeta.recruitment.common.Rule;
import com.achobeta.recruitment.model.Manager;
import com.sun.org.apache.xalan.internal.xsltc.dom.SortingIterator;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-11-06
 * Time: 23:15
 */
public class LimitUtils {

    /**
     * 判断管理员有木有权限
     * @param manager
     * @param rule
     * @return
     */
    public static boolean hasPower(Manager manager, int rule) {
        if(manager == null || manager.getLimitValue() == null
                || (manager.getLimitValue() & rule) == 0) {
            return false;
        }
        return true;
    }

    public static void grantPower(Manager manager, int rule) {
        if(manager == null) {
            return;
        }
        if(manager.getLimitValue() == null) {
            manager.setLimitValue(0);
        }
        int limitValue = manager.getLimitValue();
        manager.setLimitValue(limitValue | rule);
    }

    public static void invokePower(Manager manager, int rule) {
        if(manager == null || manager.getLimitValue() == null) {
            return;
        }
        int limitValue = manager.getLimitValue();
        manager.setLimitValue(limitValue & (~rule));
    }

    public static void main(String[] args) {
        Manager manager = new Manager(){{
            this.setLimitValue(-1);
        }};
        System.out.println(hasPower(manager, Rule.FEEDBACK_LIST_RULE));
        invokePower(manager, Rule.FEEDBACK_LIST_RULE);
        System.out.println(hasPower(manager, Rule.FEEDBACK_LIST_RULE));
        grantPower(manager, Rule.FEEDBACK_LIST_RULE);
        System.out.println(hasPower(manager, Rule.FEEDBACK_LIST_RULE));
    }

    public static void main1(String[] args) {
        Manager manager = new Manager(){{
            this.setLimitValue(0b100111);
        }};
        System.out.println(hasPower(manager, Rule.NOT_AWARE_COUNT_RULE));
        System.out.println(hasPower(manager, Rule.STUDENT_LIST_RULE));
        System.out.println(hasPower(manager, Rule.GET_VIRTUAL_CONFIG_RULE));
        System.out.println(hasPower(manager, Rule.SEND_MODEL_MESSAGE_RULE));
        System.out.println(hasPower(manager, Rule.UPLOAD_PICTURE_MESSAGE_RULE));
        System.out.println(hasPower(manager, Rule.FEEDBACK_LIST_RULE));
        System.out.println(hasPower(manager, Rule.SUPER_MANAGER_RULE));
    }

}
